package com.quectel.cms.controller.dept;


import com.quectel.cms.controller.BaseController;
import com.quectel.constant.global.SystemConstants;
import com.quectel.constant.global.annotation.RequirePermissions;
import com.quectel.core.module.dept.dto.DeptPersonRoleDto;
import com.quectel.core.module.dept.service.DeptPersonRoleService;
import com.quectel.util.common.Page;
import com.quectel.util.common.Request;
import com.quectel.util.common.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2022/07/27 10:30
 */
@RestController
@RequestMapping("deptPersonRole")
@Tag(name = "DeptPersonRoleController", description = "员工角色表相关api")
public class DeptPersonRoleController extends BaseController {

    @DubboReference
    private DeptPersonRoleService deptPersonRoleService;

    /**
     * 列表
     */
    @GetMapping("queryAll")
    @Operation(summary = "员工角色的查询列表")
    public Response<List<DeptPersonRoleDto>> queryAll() {
        //查询列表数据
        Request request = Request.configParams(Request.parseParams(httpServletRequest));

        super.fillAuthorityData(request.getParams());

        List<DeptPersonRoleDto> list = deptPersonRoleService.queryList(request.getParams());
        return Response.<List<DeptPersonRoleDto>>ok().wrap(list);
    }


    /**
     * 列表
     */
    @GetMapping("list")
    @RequirePermissions(values = "deptPersonRole:list")
    @Operation(summary = "员工角色的分页查询列表")
    public Response<Page<DeptPersonRoleDto>> list(
            @Parameter(description = "从1开始的页码数") @RequestParam Integer pageNo,
            @Parameter(description = "每页多少条") @RequestParam Integer pageSize
    ) {
        //查询列表数据
        Request request = Request.configParams(Request.parseParams(httpServletRequest));

        super.fillAuthorityData(request.getParams());
        int total = deptPersonRoleService.queryTotal(request.getParams());

        List<DeptPersonRoleDto> list = null;
        if (total > 0) {
            list = deptPersonRoleService.queryList(request.getParams());
        } else {
            list = new ArrayList<>();
        }

        Page<DeptPersonRoleDto> page = new Page<>(request.getPageNo(), request.getPageSize(), total, list);
        return Response.<Page<DeptPersonRoleDto>>ok().wrap(page);
    }


    /**
     * 编辑反显查询
     */
    @GetMapping("infoForEdit/{id}")
    @RequirePermissions(values = "deptPersonRole:edit")
    @Operation(summary = "查询单条员工角色记录")
    public Response<DeptPersonRoleDto> infoForEdit(@PathVariable("id") Long id) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));
        if (deptPersonRoleService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }

        DeptPersonRoleDto deptPersonRoleDto = deptPersonRoleService.selectById(id);

        return Response.<DeptPersonRoleDto>ok().wrap(deptPersonRoleDto);
    }

    /**
     * 信息
     */
    @GetMapping("info/{id}")
    @RequirePermissions(values = "deptPersonRole:info")
    @Operation(summary = "查询单条员工角色记录")
    public Response<DeptPersonRoleDto> info(@PathVariable("id") Long id) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));
        if (deptPersonRoleService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }

        DeptPersonRoleDto deptPersonRoleDto = deptPersonRoleService.selectById(id);

        return Response.<DeptPersonRoleDto>ok().wrap(deptPersonRoleDto);
    }

    /**
     * 保存
     */
    @PostMapping("save")
    @RequirePermissions(values = "deptPersonRole:edit")
    @Operation(summary = "保存单条员工角色记录")
    public Response<Object> save(@RequestBody @Validated DeptPersonRoleDto deptPersonRoleDto) {

        //填充基础数据
        fillSaveCommonData(deptPersonRoleDto);

        deptPersonRoleService.save(deptPersonRoleDto);

        return Response.ok();
    }

    /**
     * 修改
     */
    @PostMapping("update")
    @RequirePermissions(values = "deptPersonRole:edit")
    @Operation(summary = "修改单条员工角色记录")
    public Response<Object> update(@RequestBody @Validated DeptPersonRoleDto deptPersonRoleDto) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(deptPersonRoleDto.getId()));
        if (deptPersonRoleService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }
        //填充基础数据
        fillEditCommonData(deptPersonRoleDto);

        deptPersonRoleService.updateById(deptPersonRoleDto);

        return Response.ok();
    }

    /**
     * 删除
     */
    @PostMapping("delete")
    @RequirePermissions(values = "deptPersonRole:delete")
    @Operation(summary = "批量删除存在的员工角色记录")
    public Response<Object> delete(@RequestBody Long[] ids) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);

        for (Long id : ids) {

            params.remove(SystemConstants.ENTITY_ID_FLAG);
            params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));

            if (deptPersonRoleService.queryTotal(params) <= 0) {
                return Response.error(SystemConstants.ILLEGAL_MSG);
            }
        }

        deptPersonRoleService.deleteBatch(ids);

        return Response.ok();
    }
}
